var flag = false;
$(function () {
    var url = "/except/usercontroller/getuserinfo.action";
    var token = $("#token").val();
    if (isempty(token))
        token = "null";
    var jsonstr = "{token:" + token + "}";
    $.ajax({
        url: url,
        type: 'post',
        dataType: "json",
        data: {
            json: jsonstr
        },
        async: false,
        success: function (data) {
            var code = data.code;
            var message = data.message
            if (code == 20000) {
                $("#headimg").attr("src", data.data.headimg);
                $("#nickname").val(data.data.nickname);
                var sex = data.data.sex;
                if (sex == "2") {
                    $("#sex").html("女");
                    $("#hidesex").val(2);
                } else {
                    $("#hidesex").val(1);
                    $("#sex").html("男");
                }
                var phone = data.data.phone;
                if (isempty(phone))
                    $("#phone").val("去绑定");
                else {
                    $("#binding_phone").attr("onclick", "");
                    $("#phone").val(phone);
                }
                $("#birthday").html(data.data.birthday);
                $("#birthdaydate").val(data.data.birthday);
                remove_advertising();
            } else if (code == 10001 || code == 10002) {
                location.href = "/except/login.jsp";
            } else{
                $("#hintbg").show();
                $("#hint_message").html(message);
            }
        },
        error: function (data) {
            $("#hintbg").show();
            $("#hint_message").html("网络错误");
        }
    });

   /* $("#confirm_btn").click(function () {
        if(flag)
            location.href = "/except/files/mine_page.jsp";
    });*/
});

function file_click() {
    $("#headimg_file").click();
}

function changeimg(file) {
    var objUrl = getObjectURL(file.files[0]);
    if (objUrl) {
        $("#headimg").attr("src", objUrl);
    }
}

//建立一个可存取到该file的url
function getObjectURL(file) {
    var url = null;
    if (window.createObjectURL != undefined) { // basic
        url = window.createObjectURL(file);
    } else if (window.URL != undefined) { // mozilla(firefox)
        url = window.URL.createObjectURL(file);
    } else if (window.webkitURL != undefined) { // webkit or chrome
        url = window.webkitURL.createObjectURL(file);
    }
    return url;
}

function edituserinfo() {
    var url = "/except/usercontroller/edituserinfo.controller";
    var token = $("#token").val();
    if (isempty(token))
        token = "null";
    $("#json").val("{token:" + token + "}");
    var headimg = $("#headimg_file").val();
    var birthday = $("#birthday").html();
    $("#hidebirthday").val(birthday);
    if(isempty(headimg))
        url = "/except/usercontroller/edituserinfo.action";
    var options = {
        url: url,
        type: 'post',
        dataType: "json",
        success: function (data) {
            var code = data.code;
            var message = data.message;
            if (code == 10001)
                location.href = "/except/login.jsp";
            else if (code == 20000) {
                flag = true;
                $("#hintbg").show();
                $("#hint_message").html("修改成功");
            } else
                alert(message);
        },
        error: function (data) {
            var responseText = data.responseText;
            responseText = responseText.replace('<pre style="word-wrap: break-word; white-space: pre-wrap;">', '').replace('</pre>', '');
            var json = eval("(" + responseText + ")");
            var code = json.code;
            var message = json.message;
            if (code == 10001)
                location.href = "/except/login.jsp";
            else if (code == 20000){
                flag = true;
                $("#hintbg").show();
                $("#hint_message").html("修改成功");
            }else {
                $("#hintbg").show();
                $("#hint_message").html(message);
            }
        }
    };
    $("#userfrm").ajaxSubmit(options);
}

/**
 * 弹出性别
 */
function showPopup() {
    if ($(".cqchoosenum").hasClass("pop_payss")) {
        $(".cqchoosenum").removeClass("pop_payss");
        $("#background").hide();
    } else {
        $(".cqchoosenum").addClass("pop_payss");
        $("#background").show();
    }
}

function chose(sex) {
    $(".cqchoosenum").removeClass("pop_payss");
    $("#background").hide();
    if (sex == "2") {
        $("#sex").html("女");
        $("#hidesex").val(2);
    } else {
        $("#hidesex").val(1);
        $("#sex").html("男");
    }
}